/*
 * $Id: ProjectImpl.java 298 2006-07-29 16:30:17Z petterei $
 */
package remato.domain.common.impl;

import java.util.HashSet;
import java.util.Set;

import remato.common.domain.impl.internal.ProjectChild;
import remato.domain.common.Project;

/**
 * 
 * @author petterei
 * @version $Rev: 298 $
 */
public class ProjectImpl extends CategoryParentImpl implements Project, ProjectChild {

	private Set<Project> projectChildren;
	private Project projectParent;
	private boolean hasChildren;

	
	public ProjectImpl() {
		super();
		projectChildren = new HashSet<Project>();
		hasChildren = false;
	}

	public Project getProjectParent() {
        return projectParent;
    }
	
	public void setProjectParent(Project projectParent){
		this.projectParent = projectParent;
	}

    public boolean addProjectChild(Project child) {
		if (null != child.getProjectParent() && ! child.getProjectParent().equals(this) ) {
			child.getProjectParent().removeProjectChild(child);
		}
		((ProjectChild)child).setProjectParent(this);
		return projectChildren.add(child);
    }
    
	public Set<Project> getProjectChildren() {
		return projectChildren;
	}
    
	public void setProjectChildren(Set<Project> projectChildren) {
		this.projectChildren = projectChildren;
	}
	
    public boolean removeProjectChild(Project child) {
    	if (null != child.getProjectParent() && child.getProjectParent().equals(this)) {
        	((ProjectChild)child).setProjectParent(null);
    	}
    	return projectChildren.remove(child);
	}
    
	@Override
	public boolean hasChildren() {
		return hasChildren || ! projectChildren.isEmpty() || super.hasChildren();
	}
	
	@Override
	public void setHasChildren(boolean hasChildrenValue) {
		super.setHasChildren(hasChildrenValue);
		hasChildren = hasChildrenValue;
	}

}
